//package cn.xie.blog.servergateway.handler;
//
//import cn.hutool.extra.spring.SpringUtil;
//import cn.xie.blog.serverutil.util.JwtUtil;
//import cn.xie.blog.model.common.enums.response.ResponseCodeEnum;
//import cn.xie.blog.serverutil.constant.jwt.JwtContants;
//import cn.xie.blog.serverutil.util.RedisUtil;
//import jakarta.annotation.Resource;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.ServletOutputStream;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
///**
// * @description: 登出处理器
// * @author: xiaoxie
// * @date: 2025/4/30 上午11:03
// **/
//@Component
//public class LogOutHandler implements LogoutSuccessHandler {
//
//    private final Logger logger = LoggerFactory.getLogger(LogOutHandler.class);
//    @Resource
//    private ObjectMapper objectMapper;
//    @Resource
//    private JwtUtil jwtUtil;
//
//    @Override
//    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//        logger.info("用户登出成功：{}", authentication.getName());
//        logger.debug("请求URL：{}", request.getRequestURI());
//
//        String header = request.getHeader("Authorization");
//        if (StringUtils.hasText(header)) {
//            String token = header.replace("Bearer ", "");
//            String username = jwtUtil.parseClaims(token).get("username", String.class);
//
//            logger.debug("删除用户令牌：{}", username);
//            SpringUtil.getBean(RedisUtil.class).del(JwtContants.TOKEN_PREFIX + username);
//            SpringUtil.getBean(RedisUtil.class).del(JwtContants.REFRESH_TOKEN_PREFIX + username);
//
//            response.setContentType("application/json;charset=UTF-8");
//            ServletOutputStream out = response.getOutputStream();
//            out.write(objectMapper.writeValueAsString(ResponseCodeEnum.SUCCESS).getBytes(StandardCharsets.UTF_8));
//            out.flush();
//            out.close();
//        } else {
//            logger.debug("请求头中未找到令牌");
//        }
//    }
//}